/**
 * Copyright 2013-2022 the original author or authors from the Jeddict project (https://jeddict.github.io/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package io.github.jeddict.jpa.modeler.source.generator.ui;

import io.github.jeddict.jpa.modeler.properties.classmember.EntityMappingMemberPanel;
import io.github.jeddict.jpa.spec.EntityMappings;
import io.github.jeddict.jpa.spec.workspace.WorkSpace;
import io.github.jeddict.jpa.modeler.initializer.JPAModelerScene;
import org.netbeans.modeler.specification.model.document.IModelerScene;
import org.netbeans.modeler.properties.spec.ComboBoxValue;
import org.netbeans.modeler.properties.window.GenericDialog;
import static io.github.jeddict.jpa.modeler.initializer.JPAModelerUtil.JAVA_CLASS_ICON;

/**
 *
 * @author jGauravGupta
 */
public class EntityGenerationSettingDialog extends GenericDialog {

    private final JPAModelerScene scene;
    private final EntityMappings entityMappings;

    /**
     * Creates new form EntityGenerationSettingDialog
     *
     * @param scene
     * @param selectedWorkSpace
     */
    public EntityGenerationSettingDialog(JPAModelerScene scene, WorkSpace selectedWorkSpace) {
        this.scene = scene;
        entityMappings = scene.getBaseElementSpec();
        initComponents();
        entityMappingPanel = (EntityMappingMemberPanel) classGenerationPanel;
        entityMappingPanel.init();
        entityMappingPanel.setClassCheckable(jc -> {
            if (selectedWorkSpace != null) {
                return selectedWorkSpace.hasItem(jc);
            } else {
                return jc.getGenerateSourceCode();
            }
        });
        entityMappingPanel.setValue(entityMappings);
        loadWorkSpaceType();
        setWorkSpaceType(selectedWorkSpace);
    }

    private void loadWorkSpaceType() {
        workSpace_ComboBox.addItem(new ComboBoxValue(null, ""));
        for (WorkSpace workSpace : scene.getBaseElementSpec().getWorkSpaces()) {
            workSpace_ComboBox.addItem(new ComboBoxValue(workSpace, workSpace.getName()));
        }
    }

    private void setWorkSpaceType(WorkSpace workSpace) {
        if (workSpace != null) {
            for (int i = 0; i < workSpace_ComboBox.getItemCount(); i++) {
                if (((ComboBoxValue<WorkSpace>) workSpace_ComboBox.getItemAt(i)).getValue() == workSpace) {
                    workSpace_ComboBox.setSelectedIndex(i);
                }
            }
        } else {
            workSpace_ComboBox.setSelectedIndex(0);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        classGenerationPanel = new EntityMappingMemberPanel("Generate Entities", scene);
        action_LayeredPane = new javax.swing.JLayeredPane();
        save_Button = new javax.swing.JButton();
        cancel_Button = new javax.swing.JButton();
        workSpace_LayeredPane = new javax.swing.JLayeredPane();
        workSpace_Label = new javax.swing.JLabel();
        workSpace_ComboBox = new javax.swing.JComboBox<>();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(org.openide.util.NbBundle.getMessage(EntityGenerationSettingDialog.class, "EntityGenerationSettingDialog.title")); // NOI18N
        setIconImage(JAVA_CLASS_ICON);

        classGenerationPanel.setPreferredSize(new java.awt.Dimension(0, 200));

        javax.swing.GroupLayout classGenerationPanelLayout = new javax.swing.GroupLayout(classGenerationPanel);
        classGenerationPanel.setLayout(classGenerationPanelLayout);
        classGenerationPanelLayout.setHorizontalGroup(
            classGenerationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        classGenerationPanelLayout.setVerticalGroup(
            classGenerationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 385, Short.MAX_VALUE)
        );

        org.openide.awt.Mnemonics.setLocalizedText(save_Button, org.openide.util.NbBundle.getMessage(EntityGenerationSettingDialog.class, "EntityGenerationSettingDialog.save_Button.text")); // NOI18N
        save_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                save_ButtonActionPerformed(evt);
            }
        });
        setDefaultButton(save_Button);

        org.openide.awt.Mnemonics.setLocalizedText(cancel_Button, org.openide.util.NbBundle.getMessage(EntityGenerationSettingDialog.class, "EntityGenerationSettingDialog.cancel_Button.text")); // NOI18N
        cancel_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_ButtonActionPerformed(evt);
            }
        });

        action_LayeredPane.setLayer(save_Button, javax.swing.JLayeredPane.DEFAULT_LAYER);
        action_LayeredPane.setLayer(cancel_Button, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout action_LayeredPaneLayout = new javax.swing.GroupLayout(action_LayeredPane);
        action_LayeredPane.setLayout(action_LayeredPaneLayout);
        action_LayeredPaneLayout.setHorizontalGroup(
            action_LayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(action_LayeredPaneLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(save_Button)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancel_Button))
        );
        action_LayeredPaneLayout.setVerticalGroup(
            action_LayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(action_LayeredPaneLayout.createSequentialGroup()
                .addGroup(action_LayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(save_Button)
                    .addComponent(cancel_Button))
                .addGap(0, 13, Short.MAX_VALUE))
        );

        org.openide.awt.Mnemonics.setLocalizedText(workSpace_Label, org.openide.util.NbBundle.getMessage(EntityGenerationSettingDialog.class, "EntityGenerationSettingDialog.workSpace_Label.text")); // NOI18N

        workSpace_ComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                workSpace_ComboBoxActionPerformed(evt);
            }
        });

        workSpace_LayeredPane.setLayer(workSpace_Label, javax.swing.JLayeredPane.DEFAULT_LAYER);
        workSpace_LayeredPane.setLayer(workSpace_ComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout workSpace_LayeredPaneLayout = new javax.swing.GroupLayout(workSpace_LayeredPane);
        workSpace_LayeredPane.setLayout(workSpace_LayeredPaneLayout);
        workSpace_LayeredPaneLayout.setHorizontalGroup(
            workSpace_LayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(workSpace_LayeredPaneLayout.createSequentialGroup()
                .addComponent(workSpace_Label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(workSpace_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(52, Short.MAX_VALUE))
        );
        workSpace_LayeredPaneLayout.setVerticalGroup(
            workSpace_LayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(workSpace_LayeredPaneLayout.createSequentialGroup()
                .addGroup(workSpace_LayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(workSpace_Label)
                    .addComponent(workSpace_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(classGenerationPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(workSpace_LayeredPane)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(action_LayeredPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(classGenerationPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(action_LayeredPane)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(workSpace_LayeredPane)
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void save_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_save_ButtonActionPerformed
        entityMappings.getJavaClass()
                .forEach(javaClass -> javaClass.setGenerateSourceCode(false));
        entityMappingPanel.getSelectedJavaClass()
                .forEach(javaClass -> javaClass.setGenerateSourceCode(true));
        scene.getModelerPanelTopComponent().changePersistenceState(false);
        saveActionPerformed(evt);
    }//GEN-LAST:event_save_ButtonActionPerformed

    private void cancel_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_ButtonActionPerformed
        cancelActionPerformed(evt);
    }//GEN-LAST:event_cancel_ButtonActionPerformed

    private void workSpace_ComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_workSpace_ComboBoxActionPerformed
        WorkSpace workSpace = ((ComboBoxValue<WorkSpace>) workSpace_ComboBox.getSelectedItem()).getValue();
        if (workSpace != null) {
            entityMappingPanel.setClassCheckable(jc -> workSpace.hasItem(jc));
            entityMappingPanel.setValue(entityMappings);
        }
    }//GEN-LAST:event_workSpace_ComboBoxActionPerformed

    private final EntityMappingMemberPanel entityMappingPanel;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLayeredPane action_LayeredPane;
    private javax.swing.JButton cancel_Button;
    private javax.swing.JPanel classGenerationPanel;
    private javax.swing.JButton save_Button;
    private javax.swing.JComboBox<ComboBoxValue> workSpace_ComboBox;
    private javax.swing.JLabel workSpace_Label;
    private javax.swing.JLayeredPane workSpace_LayeredPane;
    // End of variables declaration//GEN-END:variables
}
